package com.csh.subject.domain.handler.subject;

import com.csh.subject.common.enums.IsDeletedFlagEnum;
import com.csh.subject.common.enums.SubjectInfoTypeEnum;
import com.csh.subject.domain.convert.SubjectMultipleConvert;
import com.csh.subject.domain.entity.SubjectAnswerBo;
import com.csh.subject.domain.entity.SubjectInfoBo;
import com.csh.subject.domain.entity.SubjectOptionBo;
import com.csh.subject.infra.basic.entity.SubjectMultiple;
import com.csh.subject.infra.basic.service.SubjectMultipleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 */
@Component
@Slf4j
public class MultipleTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        if (subjectInfoBo.getOptionList() != null) {
            //多选题目的插入
            List<SubjectMultiple> subjectMultipleList = new LinkedList<>();
            subjectInfoBo.getOptionList().forEach(option -> {
                SubjectMultiple subjectMultiple = SubjectMultipleConvert.INSTANCE.convertBoToMultiple(option);
                subjectMultiple.setSubjectId(subjectInfoBo.getId());
                subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
                subjectMultipleList.add(subjectMultiple);
            });
            subjectMultipleService.batchInsert(subjectMultipleList);
        }else {
            log.error("多选题插入失败，题目选项不能为空");
        }
    }

    @Override
    public SubjectOptionBo query(Long subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        List<SubjectMultiple> result = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBo> subjectAnswerBOList = SubjectMultipleConvert.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBOList);
        return subjectOptionBo;

    }
}
